<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Ads_zone_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $zone ="";
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	
	function setId($input){
		$this->id=$input;
	}
	function setZone($input){
		$this->zone=$input;
	}
	function getId(){
		return $this->id;
	}
	function getZone(){
		return $this->zone;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		if($cmd == 'id'){
			$result = $ci->db->where('id',$input)->get(TBL_ADS_ZONE)->result();
		}else if($cmd == 'zone'){
			$result = $ci->db->where('zone',$input)->get(TBL_ADS_ZONE)->result();
		}else{
			return false;
		}
		if( !empty($result) ){
			$this->id = $result[0]->id;
			$this->zone = $result[0]->zone;
			return $this;
		}
		return false;
	}
	function getAdsZoneList(){
		$ci=& get_instance();
		return $ci->db->get(TBL_ADS_ZONE)->result();
	}
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('zone',$this->getZone());
		$ci->db->insert(TBL_ADS_ZONE);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('zone', $this->getZone());
		return $ci->db->where('id', $this->id)->update(TBL_ADS_ZONE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_ADS_ZONE);
	}
	
	function getBannerByCode($code){
		$ci=& get_instance(); 
		$ci->db->set_dbprefix('default_');
		
		$zone = $ci->db->where(array('code'=>$code,'site_ids'=>get_site_id()))->get(TBL_ADS_ZONE)->result();
		if(!$zone) return '';
		$banner = $ci->db->where(array('ads_zone_id'=>$zone[0]->id,'active'=>1))->get(TBL_BANNER)->result();
	 
		$array = array();
		foreach($banner as $item){
			$follow = ($item->follow == 1) ? 'rel="me"':'rel="nofollow"';
			if($item->banner_type == 'script'){
				if($item->display_after_login == 1){
					if(isLogin()){
						$array[] = $item->script;
					}
				}else{
					$array[] = $item->script;
				}
			}
			
			if($item->banner_type == 'text'){
				if($item->display_after_login == 1){
					if(isLogin()){
						if($item->link){
							$array[] = "<a href='".$item->link."' class='bannerclicked' id='".$item->id."' $follow>".language_translate($item->define_text)."</a>";
						}else{
							$array[] = language_translate( $item->define_text );
						}
					}
				}else{
					if($item->link){
						$array[] = "<a href='".$item->link."' class='bannerclicked' id='".$item->id."' $follow>". language_translate( $item->define_text )."</a>";
					}else{
						$array[] = language_translate( $item->define_text );
					}
				}
			}
			
			if($item->banner_type == 'image'){
				$dir = site_url()."uploads/default/files/banners/".$item->image;
				if($item->display_after_login == 1){
					if(isLogin()){
						if($item->link){
							$array[] = "<a href=".$item->link." class='bannerclicked' id='".$item->id."' $follow><img src='".$dir."' height='".$zone[0]->height."' width='".$zone[0]->width."'/></a>";
						}else{
							$array[] = "<img src='".$dir."' height='".$zone[0]->height."' width='".$zone[0]->width."'/>";
						}
					}
				}else{
					if($item->link){
						$array[] = "<a href='".$item->link."' class='bannerclicked' id='".$item->id."' $follow><img src='".$dir."' height='".$zone[0]->height."' width='".$zone[0]->width."'/></a>";
					}else{
						$array[] = "<img src='".$dir."' height='".$zone[0]->height."' width='".$zone[0]->width."'/>";
					}
				}
			}
		}
		return $array;
	}
	
	//endclass
}